import React from 'react'
import Loading from '@antd/loading'
// ===================================================================== global declare
const { $http, $fn, $async } = window
// ===================================================================== global antd

// ===================================================================== global template
const message = import('@antd/message')
// ===================================================================== private template
const Page = $async(()=>import('#tp/page-content'))
// ===================================================================== private component
const Table = $async(()=>import('#cpt/table'))
const SubmitForm = $async(()=>import('#cpt/submit-form'))
// ===================================================================== config declare

// ===================================================================== component
export default class extends React.Component{
	state = {
		pag: {},
		pag1:{},
		data:[],
		mainData:[],
		infoData:[],
		submitBase:[],
		submitKind:[],
		selectedKeys: [],
		hospitalData:[],
		btns : [
			{ label:'保存', disabled:true, onClick:() => {
				const { submitBase, submitKind} = this.state
				const baseParam = $fn.getSubmit(submitBase)
				const kindParam = $fn.getSubmit(submitKind)
				
				$http.submit(this,'card/save',{ param:{...baseParam, id:this.id, kind_info:kindParam} }).then(data=>{
					message.then(f=>f.default.success('保存成功'))
				})
			} },
			{ label:'生成报告单', disabled:true, onClick:() => {
				const ids = this.state.selectedKeys.map(v=>v.id)
				$http.pull(null,'report/create', { param:{id:ids}, loading:true, loadingText:'生成报告单中...' }).then(data=>{
					if(data !== -1){
						message.then(f=>f.default.success('生成报告单成功'))
					}else{
						message.then(f=>f.default.warn('生成报告单失败'))
					}
				})
			} },
			{ label:'同步', onClick:() => {
				const ids = this.state.selectedKeys.map(v=>v['id'])
				$http.submit(this,'card/pull', { param:{ spec_code: ids }, loadingText:'数据同步中...' }).then(data=>{
					message.then(f=>f.default.success('数据同步成功'))
					this.fetch(this.model)
					this.disBtns()
				})
			} },
		],
		key:0
	}
	componentDidMount(){
		this.select()
		this.fetch1()
	}
	
	// select
	select = async () => {
		// 医院
		const hospId = await $fn.getSelect('dict/hospital')
		// 性别
		const sex = await $fn.getSelect('dict/sex')
		// 受检人类型
		const caseType = await $fn.getSelect('dict/specType')
		this.setState({ hospitalData: hospId, sexData: sex, caseTypeData:caseType})
	}
	
	model = {}
	// paging
	fetch1 = param => $http.paging(this,'card/index',{ param:{...param }, pag:'pag1', dataName:'mainData', pagingLoading:'mainPagingLoading'} )
	// paging
	fetch = param => $fn.fetch.call(this,'card/subCodeList', param)
	// select
	
	mainCols = [
		{ title: '标本条码', 	field: 'old_spec_code', 		width:110, align:'tc' },
		{ title: '自然项目', 	field: 'parent_kind_name'},
	]
	
	cols = [
		{ type:'checkbox' },
		{ title: '标本条码', 	field: 'spec_code', 		width:160, align:'tc' },
		{ title: '自然项目', 	field: 'hosp_name', 		width:150 },
		{ title: '实验号', 		field: 'experiment_num', 	width:150, align:'tc' },
		{ title: '采样时间', 	field: 'coll_time', 		width:200, align:'tc' },
		{ title: '项目名称', 	field: 'kind_name',			width:250},
		{ title: '姓名', 		field: 'patient_name',		width:250},
		{ title: '结果', 		field: 'result',			width:150},
	]
	
	render(){
		const { btns, data, mainData, pagingLoading, pullLoading, mainPagingLoading, pag, pag1, submitBase, submitKind, key } = this.state
		return (
			<Page title='检测项目' btns={btns}>
				<div className='fx ex'>
					<div className='fv' style={{width:530}}>
						<Table
							cols	= { this.mainCols }
							data 	= { mainData }
							pag		= { pag1 }
							loading = { mainPagingLoading }
							onRow	= { rows => {
								this.fetch({old_spec_code: rows.old_spec_code})
							}}
							onChange= { (current, pageSize) => {
								this.fetch1({pageSize, current})
							} }
						/>
					</div>
					<div className='ex fv ml10'>
						<Table
							cols		= { this.cols }
							data 		= { data }
							loading 	= { pagingLoading }
							pag			= { pag }
							onChange	= { (current, pageSize) => $fn.pageChange.call(this,{current, pageSize}) }
							onRow		= { async (selectedKeys, rows) => {
								let submitBase = []
								let submitKind = []
								if(rows){
									const { hospitalData, sexData, caseTypeData } = this.state
									this.id = rows.id
									await $http.pull(this,'card/info', { param:{id:rows.id}, dataName:null}).then(rs=>{
										const { tmpl, data } = rs
										const kind = data.kind_info
										const base = data.base
										submitBase = $fn.copy(tmpl.base)
										submitKind = $fn.copy(tmpl.kind_info)
										
										btns[0].disabled = false;
										
										submitBase.forEach(v=>{
											const { name } = v
											for(let i in base){
												if(i === name){
													v.value = base[i]
												}
											}
											if(name === 'hosp_name' || name === 'sex' || name === 'case_type'){
												v.type = 'select'
												v.idStr = 'name'
											}
											if(name === 'hosp_name'){
												v.data = hospitalData
											}else if(name === 'sex'){
												v.data = sexData
											}else if(name === 'case_type'){
												v.data = caseTypeData
											}else if(name === 'remark'){
												v.type = 'textarea'
											}else if(name === 'coll_at' || name === 'check_time' || name === 'report_at'){
												v.type = 'date-time'
											}
											
										})
										
										submitKind.forEach(v=>{
											for(let i in kind){
												if(i === v.name){
													v.value = kind[i]
												}
											}
										})
									})
								}
								
								if($fn.hasArray(selectedKeys)){
									btns[1].disabled = false;
								}else{
									btns[1].disabled = true;
								}
								
								this.setState({ selectedKeys, btns, submitBase, submitKind, key: key+1 })
							}}	
						/>
					</div>
					<div className='ml10 bor1 mb5 rel' style={{width:300}}  key={key}>
						{
							$fn.hasArray(submitBase) && (
								<div className='abs_lt wh oys scrollbar check-project-form'>
									<div className='h40 plr10 c0 bbor1 mlr5 mb10'>结果设置</div>
									<SubmitForm
										modal
										data 		= { submitKind }
										display
										mb 			= { 10 }
									 />
									<div className='h40 plr10 c0 bbor1 mlr5 mb10'>基本设置</div>
									<SubmitForm
										modal
										data 		= { submitBase }
										display
										mb 			= { 10 }
									 />
								</div>
							)
						}
						<Loading loading={pullLoading} />
					</div>
				</div>
			</Page>
		)
	}
}